package com.ming.attendance.service.impl;

import com.ming.attendance.mapper.AttendanceEventMapper;
import com.ming.attendance.mapper.EventUserMapper;
import com.ming.attendance.po.EventUser;
import com.ming.attendance.model.EventUserListModel;
import com.ming.attendance.po.EventUserOpengid;
import com.ming.attendance.po.ResponseBean;
import com.ming.attendance.service.EventUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class EventUserServiceImpl implements EventUserService {
    @Autowired
    private EventUserMapper eum;
    @Autowired
    private AttendanceEventMapper em;

    @Override
    public ResponseBean<Integer> createEventUser(EventUser eventUser) {
        // 先查看是否有eventid，没有则创建第一个，身份为1，即考勤者  否则为打卡者
        List<EventUser> list = eum.getEventUserByEventid(eventUser.getEventid());
        System.out.println("EventUserServiceImpl.CreateEventUser>>>>>> " + list.size());
        if(list.size() == 0){
            eventUser.setIdentity(1);
        }else{
            eventUser.setIdentity(0);
        }

        // 当list内大于两个时，此时event已经绑定了opengid，后续添加成员需要判断打卡者的opengid是否相符，不符则不能添加
        if(list.size()>2){
            EventUserOpengid eventUserOpengid = (EventUserOpengid)eventUser;    // 将eventUser向下转型成EventUserOpengid从而调用getOpengid获得opengid
            String opengid_user = eventUserOpengid.getOpengid();
            System.out.println("opengid_user >>>>>      " + opengid_user);
            String opengid_base = em.getEvent(eventUser.getEventid()).getOpengid();
            if(!opengid_user.equals(opengid_base)){
                return new ResponseBean<>(605,"不是该群成员添加失败");
            }
        }

        // 查看是否重复添加
        for (EventUser user : list) {
            if(user.getOpenid().equals(eventUser.getOpenid())){
                return new ResponseBean<>(606,"你已绑定该事件，请直接点击打卡进入打卡页面");
            }
        }

        eventUser.setJoinTime(String.valueOf(new Date().getTime()));
        int result = eum.CreateEventUser(eventUser);
        if(result!=0){
            return new ResponseBean<>(result);
        }else{
            return new ResponseBean<>(602,"创建失败");
        }
    }

    @Override
    public ResponseBean<Integer> signUp(String eventid, String openid) {
        // 检测opengid是否一致
        int result = eum.signUp(eventid, openid, String.valueOf(new Date().getTime()));
        if(result!=0){
            return new ResponseBean<>(result);
        }else{
            return new ResponseBean<>(607,"打卡失败");
        }
    }

    @Override
    public ResponseBean<EventUser> getEventUser(String eventid, String openid) {
        EventUser eventUser = eum.getEventUser(eventid, openid);
        if(eventUser!=null){
            return new ResponseBean<>(eventUser);
        }else{
            return new ResponseBean<>(601,"未加入该事件");
        }
    }

    @Override
    public ResponseBean<EventUserListModel> getEventUsersByEventid(String eventid) {
        EventUserListModel result = new EventUserListModel();
        List<EventUser> list = eum.getEventUserByEventid(eventid);
        float checkedNumber = 0;
        int joinNumber = 0;
        EventUser waitToRemove = null;
        for (EventUser eventUser : list) {
            // 打卡者
            if(eventUser.getIdentity()==0){
                // 出勤的
                if(eventUser.getIsChecked()==1){
                    checkedNumber++;
                }
                joinNumber++;
            }else{  // 不返回考勤者
                waitToRemove = eventUser;
            }
        }
        list.remove(waitToRemove);
        result.setJoinNumber(joinNumber);
        result.setCheckedNumber((int) checkedNumber);
        result.setAttendanceRate(Double.parseDouble(String.format("%.4f",checkedNumber/joinNumber)));
        result.setList(list);
        return new ResponseBean<>(result);
    }

    @Override
    public ResponseBean<Integer> deleteEventUser(EventUser eventUser) {
        int result = eum.deleteEventUser(eventUser);
        if(result!=0){
            return new ResponseBean<>(result);
        }else{
            return new ResponseBean<>(607,"删除失败");
        }
    }
}
